package com.xc.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.pojo.SysUser;
import com.xc.service.SysUserService;
import com.xc.utils.DataGridViewResult;
import com.xc.utils.SysConstants;
import com.xc.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();//清空session
        return "redirect:/";
    }

    /**
     * 获取用户列表
     * @param userVo
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public DataGridViewResult list(UserVo userVo,Principal principal){
        SysUser loginUser = sysUserService.getUserByUserName(principal.getName());
        if (loginUser!=null && loginUser.getUserType()==2){
            userVo.setUserType(2);
        }
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<SysUser> userList = sysUserService.queryUserList(userVo);
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @ResponseBody
    @RequestMapping("/addUser")
    public String addUser(SysUser sysUser, Principal principal){
        HashMap<String, Object> map = new HashMap<String, Object>();
        //获取当前登录用户信息
        SysUser loginUser = sysUserService.getUserByUserName(principal.getName());
        //创建人
        sysUser.setCreatedBy(loginUser.getId());
        if (sysUserService.addUser(sysUser)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"添加成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUser")
    public String updateUser(SysUser sysUser, Principal principal){
        HashMap<String, Object> map = new HashMap<String, Object>();
        //获取当前登录用户信息  principal.getName()：当前的用户名
        SysUser loginUser = sysUserService.getUserByUserName(principal.getName());
        //修改人
        sysUser.setModifyBy(loginUser.getId());
        if (sysUserService.updateUser(sysUser)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"添加成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(sysUserService.deleteUser(id)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"删除成功");
        }else{
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/resetPwd")
    public String resetPwd(Integer id,Principal principal){
        SysUser loginUser = sysUserService.getUserByUserName(principal.getName());

        Map<String,Object> map = new HashMap<String,Object>();
        if(sysUserService.resetPwd(id,loginUser.getId())>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"密码重置成功");
        }else{
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"密码重置失败");
        }
        return JSON.toJSONString(map);
    }
}
